package com.xing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.DTO.MenuSaveDTO;
import com.xing.DTO.MenuUpdateDTO;
import com.xing.Utils.Result;
import com.xing.dao.SysMenuDao;
import com.xing.entity.SysMenuEntity;
import com.xing.entity.SysRoleEntity;
import com.xing.service.SysMenuService;
import com.xing.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {

    @Autowired
    private SysMenuDao menuDao;

    @Override
    public List<SysMenuEntity> listAll() {
        // 封装查询条件
        LambdaQueryWrapper<SysMenuEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(SysMenuEntity.class)
                .eq(SysMenuEntity::getDelFlag, 0);
        // 查询所有
        return menuDao.selectList(lambdaQueryWrapper);
    }

    @Override
    public PageData<SysMenuEntity> listByPage(Integer pageNum, Integer pageSize) {
        // 封装查询条件
        LambdaQueryWrapper<SysMenuEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(SysMenuEntity.class)
                .eq(SysMenuEntity::getDelFlag, 0);
        // 封装分页条件
        Page<SysMenuEntity> menuEntityPage = new Page<>(pageNum, pageSize);
        // 分页查询
        menuEntityPage = menuDao.selectPage(menuEntityPage, lambdaQueryWrapper);
        // 封装返回信息
        return new PageData<>(menuEntityPage.getRecords(), menuEntityPage.getTotal());
    }

    @Override
    public Result saveMenu(MenuSaveDTO menuSaveDTO) {
        // 判断信息是否完善
        if (menuSaveDTO.getName() != null && menuSaveDTO.getUrl() != null) {
            // 复制到对应实体类
            SysMenuEntity sysMenuEntity = BeanUtil.toBean(menuSaveDTO, SysMenuEntity.class);
            // 执行插入操作
            int insert = menuDao.insert(sysMenuEntity);
            if (insert < 1) {
                return new Result().err(456, "插入失败");
            }
            return new Result().ok("插入成功");
        } else {
            return new Result().err(406, "所需信息缺失");
        }
    }

    @Override
    public Result updateById(MenuUpdateDTO menuUpdateDTO) {
        // 判断必要条件是否缺失
        if (menuUpdateDTO.getId() == null) {
            return new Result().err(406, "所需信息缺失");
        }
        // 执行更新操作
        int update = menuDao.updateById(BeanUtil.toBean(menuUpdateDTO, SysMenuEntity.class));
        if (update < 1) {
            return new Result().err(456, "修改失败");
        }
        return new Result().ok("修改成功");
    }

    @Override
    public Result deleteById(Long id) {
        SysMenuEntity sysMenuEntity = menuDao.selectById(id);
        if (sysMenuEntity == null) {
            return new Result().err(457, "无效id");
        } else {
            sysMenuEntity.setDelFlag(1);
            int update = menuDao.updateById(sysMenuEntity);
            if (update < 1) {
                return new Result().err(456, "删除失败");
            }
            return new Result().ok("删除成功");
        }
    }


}
